﻿using KidChoreApp.Data.IDataAdapters.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KidChoreApp.DataModels;

namespace KidChoreApp.Data.IDataAdapters.Adapters
{
    public class KidChoreAdapter : IKidChoreAdapter
    {
        public List<DataModels.Kid> GetAllKids()
        {
            List<Kid> kids;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                kids = db.Kids.OrderBy(x => x.Name).ToList();

            }
            return (kids);
        }

        public DataModels.Kid AddKid(DataModels.Kid kid)
        {

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                kid = db.Kids.Add(kid);
                db.SaveChanges();
            }
            return kid;
        }

        public DataModels.Kid GetKid(int id)
        {
            Kid kid;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                kid = db.Kids.Find(id);
            }
            return kid;
        }

        public DataModels.Kid UpdateKid(DataModels.Kid kid)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Kid DbKid = db.Kids.Find(kid.Id);
                DbKid.Name = kid.Name;
                DbKid.Teacher = kid.Teacher;
                DbKid.Grade = kid.Grade;
                db.SaveChanges();
            }
            return kid;
        }

        public void DeleteKid(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Kid kid = db.Kids.Find(id);
                //remove all connections to chores of kid
                List<KidChore> kc = db.KidChores.Where(x => x.KidId == kid.Id).ToList();
                db.KidChores.RemoveRange(kc);

                db.Kids.Remove(kid);
                db.SaveChanges();
            }
        }

        public List<DataModels.Chore> GetChoresForKid(int kidId)
        {
            List<Chore> Chores = new List<Chore>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                List<KidChore> KidChores = db.KidChores.Where(x => x.KidId == kidId).ToList();
                foreach (var i in KidChores)
                {
                    Chores.Add(db.Chores.Where(x => x.Id == i.ChoreId).FirstOrDefault());
                }

            }
            return Chores;
        }

        public List<DataModels.Chore> GetAllChores()
        {
            List<Chore> chores;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                chores = db.Chores.OrderBy(x => x.Name).ToList();


            }
            return chores;

        }

        public DataModels.Chore AddChore(DataModels.Chore chore)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                chore = db.Chores.Add(chore);
                db.SaveChanges();
            }
            return chore;
        }

        public DataModels.Chore GetChore(int id)
        {
            Chore chore;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                chore = db.Chores.Find(id);
            }
            return chore;
        }

        public DataModels.Chore UpdateChore(DataModels.Chore chore)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Chore newChore = db.Chores.Find(chore.Id);
                newChore.Name = chore.Name;
                newChore.Room = chore.Room;
                newChore.IsDone = chore.IsDone;
                newChore.IsRecurring = chore.IsRecurring;
                db.SaveChanges();

            }
            return chore;
        }

        public void DeleteChore(int id)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Chore oldChore = db.Chores.Find(id);
                //remove all connections to kids of chore
                List<KidChore> kc = db.KidChores.Where(x => x.ChoreId == oldChore.Id).ToList();
                db.KidChores.RemoveRange(kc);

                db.Chores.Remove(oldChore);
                db.SaveChanges();

            }
        }


        public List<DataModels.Kid> GetKidsForChore(int choreId)
        {
            List<Kid> kids = new List<Kid>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                List<KidChore> kc = db.KidChores.Where(x => x.ChoreId == choreId).ToList();
                foreach (var i in kc)
                {
                    kids.Add(db.Kids.Where(x => x.Id == i.KidId).FirstOrDefault());
                }

            }
            return kids;
        }
        public DataModels.KidChore AddKidChore(DataModels.Kid kid, DataModels.Chore chore)
        {
            KidChore kc = new KidChore();
            kc.KidId = kid.Id;
            kc.ChoreId = chore.Id;

            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                kc = db.KidChores.Add(kc);
                db.SaveChanges();
            }
            return kc;
        }



        public void DeleteKidChore(int KidId, int ChoreId)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                KidChore kc = db.KidChores.Where(x => x.ChoreId == ChoreId && x.KidId == KidId).FirstOrDefault();
                db.KidChores.Remove(kc);
                db.SaveChanges();
            }
        }
        
    }
}
